<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class ProductFactory extends Factory
{
    public function definition()
    {
        // 从数据库中随机取一个类目
        $category = \App\Models\Category::query()->where('is_directory', false)->inRandomOrder()->first();
        return [
            'title' => $this->faker->word,
            'description' => $this->faker->sentence,
            'image' => 'http://shopmall.test/fakers/images/'.rand(1,13).'.png',
            'on_sale' => true,
            'rating' => rand(0,5),
            'sold_count'=>0,
            'review_count'=>0,
            'price'=>0,
            // 将取出的类目 ID 赋给 category_id 字段
            // 如果数据库中没有类目则 $category 为 null，同样 category_id 也设成 null
            'category_id'  => $category ? $category->id : null,
        ];
    }
}
